/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package umlapp.swingui.usecasediagram;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import umlapp.Drawable;
import umlapp.elements.AElement;
import umlapp.elements.ActorElement;
import umlapp.elements.AssociationElement;
import umlapp.elements.Dimension;
import umlapp.elements.GeneralizationElement;
import umlapp.elements.PackageElement;
import umlapp.elements.UseCaseElement;
import umlapp.swingui.EditorPanel;
import umlapp.swingui.JDialogAbout;
import umlapp.swingui.JDialogDelete;
import umlapp.swingui.JDialogExit;
import umlapp.swingui.JDialogExport;
import umlapp.swingui.JDialogLoad;
import umlapp.swingui.JDialogNew;
import umlapp.swingui.JDialogSave;
import umlapp.swingui.NameDialog;

/**
 *
 * Okno pre tvorbu diagramov prípadov použitia.
 *
 * @author Peter Chovanec
 * @author Mario Jaros
 */
public class UseCaseDiagramFrame extends javax.swing.JFrame {

    /**
     * Premenné uchovávajúce x-ovú a y-ovú súradnicu myši pri stlačnom tlačidle.
     */
    private int mouseDraggedX, mouseDraggedY;
    /**
     * Pomocné premenné reprezentujúce počiatočný a koncový element pri vytváraní vzťahov.
     */
    private Drawable startDrawable, endDrawable;
    /**
     * Kontrolná premenná, ktorá indikuje či je stlačené nejaké tlačidlo na myši.
     */
    private boolean isPressed = false;
    /**
     * Dialógové okná pre zmenu mena balíčka a herca.
     */
    private JDialog packageDialog, actorDialog;

    /**
     * Vytvorenie nového Frame-u
     */
    public UseCaseDiagramFrame() {
        initComponents();
        ButtonGroup group = new ButtonGroup();
        group.add(jPackageButton);
        group.add(jActorButton);
        group.add(jToggleButton3);
        group.add(jGeneralizationButton);
        group.add(jAssociationButton);
        group.add(jToggleButton6);
        group.add(jManipulationButton);
        setLocationRelativeTo(this);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new EditorPanel();
        jToolBar2 = new javax.swing.JToolBar();
        jPackageButton = new javax.swing.JToggleButton();
        jSeparator7 = new javax.swing.JToolBar.Separator();
        jActorButton = new javax.swing.JToggleButton();
        jSeparator8 = new javax.swing.JToolBar.Separator();
        jToggleButton3 = new javax.swing.JToggleButton();
        jSeparator9 = new javax.swing.JToolBar.Separator();
        jGeneralizationButton = new javax.swing.JToggleButton();
        jSeparator10 = new javax.swing.JToolBar.Separator();
        jAssociationButton = new javax.swing.JToggleButton();
        jSeparator11 = new javax.swing.JToolBar.Separator();
        jToggleButton6 = new javax.swing.JToggleButton();
        jSeparator12 = new javax.swing.JToolBar.Separator();
        jManipulationButton = new javax.swing.JToggleButton();
        jTextFieldName = new javax.swing.JTextField();
        jLabelName = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItemNew = new javax.swing.JMenuItem();
        jMenuItemOpen = new javax.swing.JMenuItem();
        jMenuItemSave = new javax.swing.JMenuItem();
        jMenuItemExport = new javax.swing.JMenuItem();
        jMenuItemAbout = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Use Case diagram editor");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel2.setPreferredSize(new java.awt.Dimension(620, 395));
        jPanel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel2MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanel2MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanel2MouseReleased(evt);
            }
        });
        jPanel2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jPanel2MouseDragged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 618, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 389, Short.MAX_VALUE)
        );

        jToolBar2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);
        jToolBar2.setPreferredSize(new java.awt.Dimension(250, 35));

        jPackageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umlapp/icons/package.gif"))); // NOI18N
        jPackageButton.setFocusable(false);
        jPackageButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jPackageButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jPackageButton);
        jToolBar2.add(jSeparator7);

        jActorButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umlapp/icons/actor.gif"))); // NOI18N
        jActorButton.setFocusable(false);
        jActorButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jActorButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jActorButton);
        jToolBar2.add(jSeparator8);

        jToggleButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umlapp/icons/useCase.gif"))); // NOI18N
        jToggleButton3.setFocusable(false);
        jToggleButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jToggleButton3);
        jToolBar2.add(jSeparator9);

        jGeneralizationButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umlapp/icons/generalization.gif"))); // NOI18N
        jGeneralizationButton.setFocusable(false);
        jGeneralizationButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jGeneralizationButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jGeneralizationButton);
        jToolBar2.add(jSeparator10);

        jAssociationButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umlapp/icons/asociation.gif"))); // NOI18N
        jAssociationButton.setFocusable(false);
        jAssociationButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jAssociationButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jAssociationButton);
        jToolBar2.add(jSeparator11);

        jToggleButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umlapp/icons/association.gif"))); // NOI18N
        jToggleButton6.setFocusable(false);
        jToggleButton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton6.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jToggleButton6);
        jToolBar2.add(jSeparator12);

        jManipulationButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umlapp/icons/handler.png"))); // NOI18N
        jManipulationButton.setToolTipText("Manipulate");
        jManipulationButton.setFocusable(false);
        jManipulationButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jManipulationButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(jManipulationButton);

        jTextFieldName.setText("name");

        jLabelName.setFont(new java.awt.Font("Lucida Fax", 1, 10)); // NOI18N
        jLabelName.setText("Enter element name:");

        jMenu1.setText("File");

        jMenuItemNew.setText("New Diagram Model");
        jMenuItemNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemNewActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemNew);

        jMenuItemOpen.setText("Open Diagram Model");
        jMenuItemOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemOpenActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemOpen);

        jMenuItemSave.setText("Save Diagram Model");
        jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemSave);

        jMenuItemExport.setText("Export to...");
        jMenuItemExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExportActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemExport);

        jMenuItemAbout.setText("About");
        jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAboutActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemAbout);

        jMenuItemExit.setText("Exit");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItemExit);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabelName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldName))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabelName, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Implementácia kliknutia myši Metóda kontroluje ktoré tlačidlo je stlačené a podľa toho
     * vykonáva ďalšie operácie Vykresľuje jednotlive elementy Pravým tlačidlom maže elementy
     * Dvojitým kliknutím vyvoláva dialógy
     *
     * @param evt
     */
    private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel2MouseClicked
        if (jActorButton.isSelected()) {
            Dimension dimension = new Dimension(evt.getX(), evt.getY());
            ((EditorPanel) jPanel2).addDrawableElement(new ActorElement(jTextFieldName.getText(), dimension));
            jPanel2.repaint();
        }
        if (jPackageButton.isSelected()) {
            Dimension dimension = new Dimension(evt.getX(), evt.getY());
            ((EditorPanel) jPanel2).addDrawableElement(new PackageElement(jTextFieldName.getText(), dimension));
            jPanel2.repaint();
        }
        if (jToggleButton3.isSelected()) {
            Dimension dimension = new Dimension(evt.getX(), evt.getY());
            ((EditorPanel) jPanel2).addDrawableElement(new UseCaseElement(jTextFieldName.getText(), dimension));
            jPanel2.repaint();
        }


        if (evt.getClickCount() == 2) {
            Drawable drawable = ((EditorPanel) jPanel2).getDrawableElement(evt.getX(), evt.getY());
            if (drawable != null) {

                if (drawable instanceof PackageElement) {
                    packageDialog = new NameDialog(this, true, (PackageElement) drawable);
                    packageDialog.setVisible(true);
                }

                if (drawable instanceof ActorElement) {
                    actorDialog = new NameDialog(this, true, (ActorElement) drawable);
                    actorDialog.setVisible(true);
                }
            }
        }

        if (SwingUtilities.isRightMouseButton(evt)) {
            Drawable drawable = ((EditorPanel) jPanel2).getDrawableElement(evt.getX(), evt.getY());

            if (drawable != null) {
                new JDialogDelete(this, true, (AElement) drawable, jPanel2).setVisible(true);
            }
        }
    }//GEN-LAST:event_jPanel2MouseClicked
    /**
     * Ak je tlacidlo s asociaciou stlacene tak pri stlaceni mysi sa nacita zaciatocny element do
     * atributu startDrawable
     */
    private void jPanel2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel2MousePressed
        if (jAssociationButton.isSelected() || jGeneralizationButton.isSelected()) {
            isPressed = true;
            startDrawable = ((EditorPanel) jPanel2).getDrawableElement(evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_jPanel2MousePressed
    /*
     * ak je tlacidlo s asociaciou stlacene a aj tlacitko mysi je stlacene a pouzivatel pusti mys na
     * nejakom elemente tak ten sa nacita do atributu endDrawable a vytvori sa novy element
     * Asociacia zatial s nulovymi rozmermi. Rozmery sa obstaravaju v metode drawElement v
     * konkretnom objekte typu AssociationElement
     */
    private void jPanel2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel2MouseReleased
        if (jAssociationButton.isSelected() && isPressed) {
            endDrawable = ((EditorPanel) jPanel2).getDrawableElement(evt.getX(), evt.getY());
            if ((startDrawable != null) && (endDrawable != null)) {
                ((EditorPanel) jPanel2).addDrawableElement(new AssociationElement(jTextFieldName.getText(), new Dimension(0, 0), startDrawable, endDrawable));
            }
        }

        if (jGeneralizationButton.isSelected() && isPressed) {
            endDrawable = ((EditorPanel) jPanel2).getDrawableElement(evt.getX(), evt.getY());
            if ((startDrawable != null) && (endDrawable != null)) {
                ((EditorPanel) jPanel2).addDrawableElement(new GeneralizationElement(jTextFieldName.getText(), new Dimension(0, 0), startDrawable, endDrawable));
            }
        }
        isPressed = false;
        jPanel2.repaint();
    }//GEN-LAST:event_jPanel2MouseReleased
    /**
     * Implementácia tahanie myši Ak je stlačené tlačidlo manipulation tak posúva elementy po panely
     *
     * @param evt
     */
    private void jPanel2MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel2MouseDragged
        mouseDraggedX = evt.getX();
        mouseDraggedY = evt.getY();

        Drawable drawable = ((EditorPanel) jPanel2).getDrawableElement(mouseDraggedX, mouseDraggedY);

        if (jManipulationButton.isSelected()) {
            if (drawable != null) {
                int x = mouseDraggedX - drawable.getDimension().width / 2;
                int y = mouseDraggedY - drawable.getDimension().height / 2;
                drawable.setLocation(x, y);
            }
        }
        jPanel2.repaint();        // TODO add your handling code here:
    }//GEN-LAST:event_jPanel2MouseDragged
    /**
     * Vyvoláva dialógové okno na vytvorenie nového modelu
     *
     * @param evt
     */
    private void jMenuItemNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemNewActionPerformed
        new JDialogNew(this, true, (EditorPanel) jPanel2).setVisible(true);
    }//GEN-LAST:event_jMenuItemNewActionPerformed
    /**
     * Vyvoláva dialógové okno na načítavanie do súboru
     *
     * @param evt
     */
    private void jMenuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemOpenActionPerformed
        new JDialogLoad(this, true, (EditorPanel) jPanel2).setVisible(true);
    }//GEN-LAST:event_jMenuItemOpenActionPerformed
    /**
     * Vyvoláva dialógove okno na uloženie do súboru
     *
     * @param evt
     */
    private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed
        new JDialogSave(this, true, (EditorPanel) jPanel2).setVisible(true);
    }//GEN-LAST:event_jMenuItemSaveActionPerformed
    /**
     * Vyvoláva dialógove okno na export do obrázka
     *
     * @param evt
     */
    private void jMenuItemExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExportActionPerformed
        new JDialogExport(this, true, (EditorPanel) jPanel2).setVisible(true);
    }//GEN-LAST:event_jMenuItemExportActionPerformed
    /**
     * Vyvoláva dialógové okno na ukončenie programu
     *
     * @param evt
     */
    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        new JDialogExit(this, true).setVisible(true);
    }//GEN-LAST:event_jMenuItemExitActionPerformed
    /**
     * Prepísana metóda na prekresľovanie panelu.
     */
    @Override
    public void repaint() {
        super.repaint();
        jPanel2.repaint();
    }

    /**
     * Vyvoláva dialógové okno About
     *
     * @param evt
     */
    private void jMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAboutActionPerformed
        new JDialogAbout(this, true).setVisible(true);
    }//GEN-LAST:event_jMenuItemAboutActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton jActorButton;
    private javax.swing.JToggleButton jAssociationButton;
    private javax.swing.JToggleButton jGeneralizationButton;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JToggleButton jManipulationButton;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItemAbout;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemExport;
    private javax.swing.JMenuItem jMenuItemNew;
    private javax.swing.JMenuItem jMenuItemOpen;
    private javax.swing.JMenuItem jMenuItemSave;
    private javax.swing.JToggleButton jPackageButton;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToolBar.Separator jSeparator10;
    private javax.swing.JToolBar.Separator jSeparator11;
    private javax.swing.JToolBar.Separator jSeparator12;
    private javax.swing.JToolBar.Separator jSeparator7;
    private javax.swing.JToolBar.Separator jSeparator8;
    private javax.swing.JToolBar.Separator jSeparator9;
    private javax.swing.JTextField jTextFieldName;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToggleButton jToggleButton6;
    private javax.swing.JToolBar jToolBar2;
    // End of variables declaration//GEN-END:variables
}
